#include <stdio.h>
#include <stdlib.h>

#include "nuclei_sdk_hal.h"
#include "nuclei_sdk_soc.h"

#define AHB_SRAM_ADDRESS 0x80000000

#if 0
int main(void)
{
    for(int32_t i =0 ; i <10 ; i++)
    {
        REG32(AHB_SRAM_ADDRESS+i*4) = 0x5a5a5a5+i;
    }

    for(int32_t j =0 ; j <10 ; j++)
    {
        if(REG32(AHB_SRAM_ADDRESS+j*4) != (0x5a5a5a5+j))
        {
            printf("AHB_SRAM access error:address :%x\r\n",AHB_SRAM_ADDRESS+j);
            printf("REG32(0x%x)=0x%x ! 0x%x\r\n",AHB_SRAM_ADDRESS+j*4,REG32(AHB_SRAM_ADDRESS+j*4), 0x5a5a5a5+j);
            return -1;
        }
        else
		{
			printf("REG32(0x%x)=0x%x\r\n",AHB_SRAM_ADDRESS+j*4,0x5a5a5a5+j);
		}
    }
    #ifdef CFG_SIMULATION
        pass_fail_simulation(1);
    #endif
    printf("AHB_SRAM access PASS\r\n");
#else

#define LEDn  3

static uint32_t LED_CLORK[] = {SOC_LED_RED_GPIO_MASK, SOC_LED_GREEN_GPIO_MASK,SOC_LED_BLUE_GPIO_MASK};

void led_config(void)
{
    for(int i=0; i<LEDn; i++)
    {
        //gpio_iof_config(GPIO,LED_CLORK[i],IOF_SEL_DEF);
        gpio_enable_output(GPIO,LED_CLORK[i]);
        gpio_write(GPIO,LED_CLORK[i],1);
    }
}

int main(void)
{
	led_config();
	for(int32_t i =0 ; i <10 ; i++)
	{
		REG32(AHB_SRAM_ADDRESS+i*4) = 0x5a5a5a5+i;
	}

	for(int32_t j =0 ; j <10 ; j++)
	{
		if(REG32(AHB_SRAM_ADDRESS+j*4) != (0x5a5a5a5+j))
		{
//			printf("AHB_SRAM access error:address :%x\r\n",AHB_SRAM_ADDRESS+j);
//			printf("REG32(0x%x)=0x%x ! 0x%x\r\n",AHB_SRAM_ADDRESS+j*4,REG32(AHB_SRAM_ADDRESS+j*4), 0x5a5a5a5+j);
			return -1;
		}
		else
		{
//			printf("REG32(0x%x)=0x%x\r\n",AHB_SRAM_ADDRESS+j*4,0x5a5a5a5+j);
		}
	}

	while(1)
	{
		for(int i=0; i<LEDn; i++)
		{
			gpio_toggle(GPIO,LED_CLORK[i]);
//            delay_1ms(500);
//            printf("gpio_toggle\r\n");
		}
	}
//	printf("AHB_SRAM access PASS\r\n");
#endif
}
